home *** CD-ROM | disk | FTP | other *** search
Wrap
/* Some of the editor code borrowed from mozilla's midas demo: http://www.mozilla.org/editor/midasdemo/ */ var WiseStampEditors = { current: null, _doc: null, get doc() { return this._doc; //return this.current.contentWindow.document; }, doCommand: function (obj) { aType = obj.getAttribute('cmdtype'); if ((aType == "fontsize") || (aType == "fontname")) { this.command = aType; } if ((aType == "forecolor") || (aType == "hilitecolor")) { this.command = aType; } if ((aType == "justifyleft") || (aType == "justifycenter") || (aType == "justifyright")) { // need to check the current state according to the cursor position in the rich editor //document.getElementById("justifyleft").className = 'unpressed'; //document.getElementById("justifycenter").className = 'unpressed'; //document.getElementById("justifyright").className = 'unpressed'; } if (aType == "createlink") { var link = WiseStampUtils.prompt("createlinkTitle", "http://", "createlink"); if (link == null) return; if (link != "") this.doc.execCommand("CreateLink", false, link); else WiseStampUtils.alert("invalid_link_error"); } else if (aType == "createimage") { imagePath = WiseStampUtils.prompt("createimageTitle", "http://", "createimage"); if (imagePath == null) return; if (imagePath != "") { var html = '<img border=0 src='+imagePath+' />'; this.doc.execCommand('InsertHTML',false,html); } else WiseStampUtils.alert("invalid_image_error"); } else if (aType == "createtable") { e = this.current; rowstext = prompt(WisestampSignatureFactory.strings.GetStringFromName("createtablerows")); colstext = prompt(WisestampSignatureFactory.strings.GetStringFromName("createtablecolumns")); rows = parseInt(rowstext); cols = parseInt(colstext); if ((rows > 0) && (cols > 0)) { table = this.doc.createElement("table"); table.setAttribute("border", "1"); table.setAttribute("cellpadding", "2"); table.setAttribute("cellspacing", "2"); tbody = this.doc.createElement("tbody"); for (var i = 0; i < rows; i++) { tr = this.doc.createElement("tr"); for (var j = 0; j < cols; j++) { td = this.doc.createElement("td"); br = this.doc.createElement("br"); td.appendChild(br); tr.appendChild(td); } tbody.appendChild(tr); } table.appendChild(tbody); this._insertNodeAtSelection(e.contentWindow, table); } } else if (aType == "ltr" || aType == "rtl") { this.selectWritingDirection(aType); } else { this.doc.execCommand(aType, false, null); } if (obj.className == 'pressed') obj.className = 'unpressed'; else if (obj.className == 'unpressed') obj.className = 'pressed'; }, _insertNodeAtSelection: function (win, insertNode) { var sel = win.getSelection(); var range = sel.getRangeAt(0); sel.removeAllRanges(); range.deleteContents(); var container = range.startContainer; var pos = range.startOffset; range = document.createRange(); if (container.nodeType == 3 && insertNode.nodeType == 3) { container.insertData(pos, insertNode.nodeValue); // put cursor after inserted text range.setEnd(container, pos + insertNode.length); range.setStart(container, pos + insertNode.length); } else { var afterNode; if (container.nodeType == 3) { var textNode = container; container = textNode.parentNode; var text = textNode.nodeValue; // text before the split var textBefore = text.substr(0, pos); // text after the split var textAfter = text.substr(pos); var beforeNode = document.createTextNode(textBefore); afterNode = document.createTextNode(textAfter); // insert the 3 new nodes before the old one container.insertBefore(afterNode, textNode); container.insertBefore(insertNode, afterNode); container.insertBefore(beforeNode, insertNode); // remove the old node container.removeChild(textNode); } else { // else simply insert the node afterNode = container.childNodes[pos]; container.insertBefore(insertNode, afterNode); } range.setEnd(afterNode, 0); range.setStart(afterNode, 0); } sel.addRange(range); }, colorPickerShowing: function (event) { this.command = document.popupNode.getAttribute("cmdtype"); return true; }, colorPickerHiding: function () { this.current.focus(); }, colorPickerClick: function (event) { var target = event.originalTarget; if (target.tagName == "box") { var color = target.id; document.getElementById("colorpicker").hidePopup(); this.doc.execCommand(this.command, false, color); this.current.contentWindow.focus(); this.command = null; } }, changeFont: function (event, mode) { var value = event.target.getAttribute("value"); this.doc.execCommand("font" + mode, false, value); }, showFontName: function (event) { var mixed = {} var font = this.editor.getFontFaceState(mixed); var ele = this.current.contentWindow.getSelection().anchorNode; if (ele) { ele = ele.parentNode; var style = this.current.contentWindow.getComputedStyle(ele, ""); var reg = /['"](.+)['"]\s*,\s*.+/ var fam = style.fontFamily.replace(reg, "$1").toLowerCase(); var found = false; Array.forEach(event.target.childNodes, function (node) { if (fam == node.value.replace(reg, "$1").toLowerCase()) { node.setAttribute("checked", "true"); found = true; } else node.removeAttribute("checked"); }); } }, selectWritingDirection: function (direction) { if (direction == "none") { //this.removeTextProperty("*","dir"); } else { this.current.contentDocument.dir = direction; //this.setTextProperty("*", "dir", direction); } }, get editor() { try { Ci = Components.interfaces; Cc = Components.classes; var win = this.current.contentWindow; var editingSession = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession); if (editingSession.windowIsEditable(win)) { var editor = editingSession.getEditorForWindow(win); return editor.QueryInterface(Components.interfaces.nsIHTMLEditor); } } catch(ex) {} return null; }, get editorForWin() { try { Ci = Components.interfaces; Cc = Components.classes; var win = this.current.contentWindow; var editingSession = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession); if (editingSession.windowIsEditable(win)) { var editor = editingSession.getEditorForWindow(win); return editor; } } catch(ex) {} return null; }, removeTextProperty: function (property, attribute) { { var atomService = Components.classes["@mozilla.org/atom-service;1"]. getService(Components.interfaces.nsIAtomService); var propAtom = atomService.getAtom(property); this.editor.removeInlineProperty(propAtom, attribute); } }, setTextProperty: function (property, attribute, value) { { var atomService = Components.classes["@mozilla.org/atom-service;1"]. getService(Components.interfaces.nsIAtomService); var propAtom = atomService.getAtom(property); this.editor.setInlineProperty(propAtom, attribute, value); } } } function WiseStampInitWYSIWYGEditor(editor, html, dir) { WiseStampUtils.log("[editor.js::WiseStampInitWYSIWYGEditor] html = " + html); WiseStampEditors.current = editor; var eles = document.getElementById("editor-toolbar-simple").childNodes; for (var i = 0; i < eles.length; i++) { var cmdtype = eles[i].getAttribute("cmdtype"); if (cmdtype) { eles[i].setAttribute("src", "chrome://wisestamp/skin/editor/" + cmdtype + ".gif"); eles[i].setAttribute("onclick", "WiseStampEditors.doCommand(event.target)"); } } if (editor.hasAttribute("initialized")) { editor.contentDocument.body.innerHTML = html; editor.contentDocument.dir = dir; editor.contentDocument.designMode = 'on'; WiseStampEditors._doc = editor.contentDocument; } else { WiseStampEditors._doc = editor.contentDocument; function initEditor() { WiseStampUtils.log("[editor.js::WiseStampInitWYSIWYGEditor] onLoad - initEditor"); editor.contentDocument.body.innerHTML = html; editor.contentDocument.dir = dir; editor.contentDocument.designMode = 'on'; WiseStampEditors._doc = editor.contentDocument; editor.setAttribute("initialized","true"); } setTimeout(initEditor,0); // event is not fired on Thunderbird - this works though editor.contentWindow.addEventListener("load",initEditor,false); } }